package com.star.wellconnect.signuplogin;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.star.wellconnect.R;
import com.star.wellconnect.WellConnectActivity;
import com.star.wellconnect.sqlite.AppPreferences;
import com.star.wellconnect.sqlite.DatabaseHelper;

public class LoginFragment extends Fragment{
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
		return inflater.inflate(R.layout.fragment_login, container, false);
	}
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState){
		super.onViewCreated(view, savedInstanceState);
		
		final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
		
		final EditText username = (EditText) view.findViewById(R.id.username);
		final EditText password = (EditText) view.findViewById(R.id.password);
		final Button login = (Button) view.findViewById(R.id.login);
		final TextView error = (TextView) view.findViewById(R.id.error);
		
		// Listener
		TextWatcher inputFieldsValidatorTextWatcher = new TextWatcher(){
			@Override
			public void afterTextChanged(Editable arg0) {}
			
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				error.setText("");
				if(isNotEmpty())
					login.setEnabled(true);
				else
					login.setEnabled(false);
			}
			
			private boolean isNotEmpty(){
				return username.getText().toString().trim().length() > 0
						&& password.getText().toString().trim().length() > 0;
			}
		};
		
		username.addTextChangedListener(inputFieldsValidatorTextWatcher);
		password.addTextChangedListener(inputFieldsValidatorTextWatcher);
		
		login.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				DatabaseHelper db = new DatabaseHelper(getActivity());
				int isValidUser = db.isValidUser(username.getText().toString().trim(), 
						password.getText().toString().trim());
				
				if(isValidUser == DatabaseHelper.TRUE){
	                error.setText("");
	                Editor editor = sharedPrefs.edit();
	                
	                editor.putBoolean(AppPreferences.IS_LOGGED_IN, true);
	                editor.putString(AppPreferences.USERNAME, username.getText().toString().trim());
	                editor.commit();
	                
	                Intent intent = new Intent(getActivity(), WellConnectActivity.class);
	                startActivity(intent);
				}else if(isValidUser == DatabaseHelper.FALSE) {
					error.setText("Username or Password Invalid");
				}else{
					error.setText("Username does not exist. Double check your username or sign up.");
				}
			}
		});
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}
}
